<template>
	<view class="main-view"  style="background-image: url('/static/images/firstbg.jpg');" v-if="showpage">
		<form @submit="submit" >
			<view class="center_view" >
				<view class="center_input_item">
					<view v-if="isshow">
						<view class="item" >
							<view class="iconfont icon-gerenzhongxin-zhanghaoguanli item-img" ></view>
							<input name="url" class="center_text" placeholder="服务器域名" placeholder-style="font-size:12px;color:#ff9d29;" 
							v-model="serverurl"></input>
						</view>
						<view class="line"></view>
						<!-- <view class="item">
							<view class="iconfont icon-dianmian item-img"></view>
							<picker class="item-picker" v-model="version"  @change="bindPickerChange2" :range="versions">
								<view v-if="version!=''" class="item-picker-view">{{version}}</view>
								<view v-else class="item-picker-view" style="color:#ff9d29;font-size: 12px;">请选择版本</view>
							</picker>
						</view>
						<view class="line"></view> -->
					</view>
					
					<view class="item">
						<view class="iconfont icon-qiye item-img" ></view>
						<input name="productCode" class="center_text" placeholder="产品名" placeholder-style="font-size:12px;color:#ff9d29;" 
						v-model="productCode"
						 @input="getprojectname($event,'product')"></input>
					</view>
					<view class="line"></view>
		
					<view class="item">
						<view class="iconfont icon-gerenzhongxin-zhanghaoguanli item-img" ></view>
						<input name="accountName" class="center_text" placeholder="账套名" placeholder-style="font-size:12px;color:#ff9d29;" 
						v-model="accountName" @input="getprojectname($event,'account')"></input>
					</view>
					<view class="line"></view>
					<view class="item">
						<view class="iconfont icon-dianmian item-img"></view>
						<picker class="item-picker" :value="projectListIndex"  @change="bindPickerChange($event,projectList)" :range="projectList" :range-key="'projectname'">
						 	<view v-if="JSON.stringify(projectList)!='[]'" class="item-picker-view">{{projectList[projectListIndex].projectname}}</view>
							<view v-else class="item-picker-view" style="color:#ff9d29;font-size: 12px;">请选择子项目</view>
						</picker>
					</view>
					<view class="line"></view>
					
				</view>
				<view>
					<button class="register" form-type="submit">确认初始化</button>
				</view>
				
			 </view>
		</form>
		
	</view>
</template>
<script>
	const app = getApp().globalData;
	var util = require("../../utils/util.js");

	export default {
		data() {
			return {
				productCode: '',
				accountName:'',
				projectCode:'',
				projectName:'',
				projectList:[],
				projectListIndex:0,
				terminaltype: '',
				enabledropdown:false,
				showpage:false,
				isshow:false,
				serverurl:'https://www.hbxtwl.com/as176',
				// serverurl:'https://www.hbxtwl.com/as230/',
				// serverurl:'http://47.104.140.185/as185/',
				versions:['正式','测试'],
				version:''
				
			};
		},


		components: {},
		props: {},
		onLoad: function(options) {
		},

		onShow: async function() {
			if(uni.getStorageSync('loginIdPre') === '666666'){
				this.isshow = true
				this.productCode = uni.getStorageSync('productCodePre')
				this.accountName = uni.getStorageSync('accountNamePre')
				if(this.accountName.indexOf('测试')!=-1){
					// this.version = '测试'
					getApp().globalData.app_address =this.serverurl + '/pbapi/'
					// getApp().globalData.fileService = 'pbfile'
				}else{
					// this.version = '正式'
					getApp().globalData.app_address = this.serverurl + '/pbapi/'
					// getApp().globalData.fileService = 'pbfile2'
					
				}
				this.projectName = uni.getStorageSync('projectNamePre')
				await this.getprojectname('','')
			}
			if(!this.isshow){
				
			//当程序要写死项目名时候 这里要开放
			// uni.setStorageSync('productCode', 'SCM')
			// uni.setStorageSync('projectCode', 'ESM')
			// uni.setStorageSync('projectName', '"总部管理系统"')
			// uni.setStorageSync('accountName', '供应链开发')
			// uni.setStorageSync('clientType','WeChat')
			
			// uni.setStorageSync('productCode', 'RTERP')
			// uni.setStorageSync('projectCode', 'SRM')
			// uni.setStorageSync('projectName', 'ERP系统')
			// uni.setStorageSync('accountName', '美星电子')
			
			// uni.setStorageSync('productCode', 'RTERP')
			// uni.setStorageSync('projectCode', 'SRM')
			// uni.setStorageSync('projectName', 'ERP系统')
			// uni.setStorageSync('accountName', '美星测试')
			
			// uni.setStorageSync('productCode', 'RTERP')
			// uni.setStorageSync('projectCode', 'ERP')
			// uni.setStorageSync('projectName', 'ERP系统')
			// uni.setStorageSync('accountName', '美星电子')
			// uni.setStorageSync('clientType', 'pda')
			// // uni.setStorageSync('clientType','WeChat')
			
			
			// uni.setStorageSync('productCode', 'RTERP')
			// uni.setStorageSync('projectCode', 'ERP')
			// uni.setStorageSync('projectName', 'ERP系统')
			// uni.setStorageSync('accountName', '美星测试')
			// // uni.setStorageSync('clientType','pda')
			// uni.setStorageSync('clientType','WeChat')
			
			
			uni.setStorageSync('productCode', 'SCM')
			uni.setStorageSync('projectCode', 'HMS')
			uni.setStorageSync('projectName', '总部管理系统')
			uni.setStorageSync('accountName', 'FamilyCity')
			// uni.setStorageSync('clientType','pda')
			uni.setStorageSync('clientType','WeChat')
			}
			var productCode = uni.getStorageSync('productCode');
			console.log(productCode)
			if (productCode === undefined || productCode === '') {
				this.showpage = true
			}else{
				this.submit();
			}
		},
		
		methods: {
			
			async getprojectname(res,type){
				var that = this
				if(type == 'product'){
					this.productCode = res.detail.value
				}
				if(type == 'account'){
					this.accountName = res.detail.value
				}
				console.log(this.accountName)
				var terminaltype = 'WeChat'
				var ss = this.productCode != '' && this.accountName != ''
			
				console.log(ss)
				if(this.productCode != '' && this.accountName != ''){
					var machineName = ''
					var mac = ''
					var ip = ''
					uni.getSystemInfo({
						success:function(e){
							machineName = e.deviceBrand
							mac = e.deviceId
						}
					})
					getApp().globalData.app_address = this.serverurl + '/pbapi/'
					var injson = '{"productCode":"' + this.productCode + '","terminalType":"' + terminaltype + '","machineName":"' + machineName + '","mac":"' + mac + '","ip":"' + ip +'","accountName":"' + this.accountName +'"}';
					await	uni.request({
						url: getApp().globalData.app_address + '?method=saas_GetProjectList&parse=json',
						data: injson,
						method: 'POST',
						header: { 'content-type': 'application/x-www-form-urlencoded' },
						success: function(res) {
							var result = res.data;
							console.log('result',result)
							if(result.rtn==1){
								that.projectList = result.data
								let ifindex = 0
								that.projectList.forEach((val,index)=>{
									if(val.projectname == that.projectName){
										ifindex += 1
										that.projectListIndex = index
										that.projectCode = val.projectcode
										that.$forceUpdate()
									}
								})
								
								if(ifindex == 0) that.projectCode = that.projectList[0].projectcode
							}
						},
						fail: function(res) {
							
						}
					});
				}
			},
			
			bindPickerChange(e, projectList){
				var that = this
				console.log(e)
				that.projectListIndex = e.detail.value
				that.projectCode = projectList[e.detail.value].projectcode
				that.projectName = projectList[e.detail.value].projectname
			},
			
			bindPickerChange2(e){
				this.version = this.versions[e.detail.value]
				if(this.isshow){
					if(this.version==''){
						uni.showModal({
						    title: '提示',
						    content: '版本不能为空',
							showCancel:false
						})
						return
					}
					var serverurl = this.serverurl
					if(serverurl==''){
						uni.showModal({
						    title: '提示',
						    content: '域名不能为空',
							showCancel:false
						})
						return
					}
					
					getApp().globalData.pic_address = serverurl + 'pbapi'
					// if(this.version == '正式'){
					// 	getApp().globalData.app_address = serverurl + '/pbapi/'
					// 	// getApp().globalData.fileService = 'pbfile2'
					// }else{
					// 	getApp().globalData.app_address = serverurl + '/pbapi/'
					// 	// getApp().globalData.fileService = 'pbfile'
					// }
				}
			},
			
			submit(res){
				var that = this
				var terminaltype = 'WeChat'
				console.log(res)
				console.log(that.projectCode)
				if(res == undefined ){
					var productCode = uni.getStorageSync('productCode')
					var accountName = uni.getStorageSync('accountName')
					var projectCode = uni.getStorageSync('projectCode')
					var projectName = uni.getStorageSync('projectName')
				}else{
					var productCode = res.detail.value.productCode
					var accountName = res.detail.value.accountName
					var projectCode = that.projectCode
					var projectName = that.projectName
				}
				if(productCode === '' || productCode === undefined){
					uni.showModal({
					    title: '提示',
					    content: '产品名不能为空',
						showCancel:false
					})
					return
				}
				if(accountName === '' || accountName === undefined){
					uni.showModal({
					    title: '提示',
					    content: '账套名不能为空',
						showCancel:false
					})
					return
				}
				if(projectCode === '' || projectCode === undefined){
					uni.showModal({
					    title: '提示',
					    content: '子项目不能为空',
						showCancel:false
					})
					return
				}
				var machineName = ''
				var mac = ''
				var ip = ''
				uni.getSystemInfo({
					success:function(e){
						machineName = e.deviceBrand + ' ' + e.deviceModel + ' ' + e.deviceType
						mac = e.deviceId
					}
				})
				getApp().globalData.app_address = this.serverurl + '/pbapi/'
				getApp().globalData.pic_address = this.serverurl
				var injson = '{"productCode":"' + productCode + '","terminalType":"' + terminaltype + '","machineName":"' + machineName + '","mac":"' + mac + '","ip":"' + ip +'","accountName":"' + accountName +'","projectCode":"' + projectCode +'"}';
				console.log(injson)
				console.log("saas_GetFirstPage")
				console.log(getApp().globalData.app_address + '?method=saas_GetFirstPage&parse=json')
				uni.request({
					url: getApp().globalData.app_address + '?method=saas_GetFirstPage&parse=json',
					data: injson,
					method: 'POST',
					header: { 'content-type': 'application/x-www-form-urlencoded' },
					success: function(res) {
						console.log(res)
						var result = res.data;
						console.log(result)
						
						uni.setStorageSync('productCode',productCode)
						uni.setStorageSync('accountName',accountName)
						uni.setStorageSync('projectCode',projectCode)
						uni.setStorageSync('projectName',projectName)
						uni.setStorageSync('terminaltype',"WeChat")
						// #ifdef APP-PLUS
						uni.setStorageSync('clientType','pda')
						// #endif
						// #ifndef APP-PLUS
						uni.setStorageSync('clientType','WeChat')
						// #endif
						uni.setStorageSync('dbId',result.data.dbId)
						var pageName = result.data.pageName
						var menuId = result.data.menuId
						var modId = result.data.modId
						var templetName = result.data.templetName
						
						if(pageName == 'appsj_welcome'){
							uni.setStorageSync('loginIdPre','')
							uni.navigateTo({
								url:'../welcome/welcome?menuId='+menuId+'&modId='+modId+'&pageName='+pageName+'&templetName='+templetName
							})
						}else if(pageName == 'appsj_logon'){
							uni.setStorageSync('loginpageName',pageName)
							uni.setStorageSync('logintempletName',templetName)
							uni.setStorageSync('loginmenuId',menuId)
							uni.setStorageSync('loginmodId',modId)
							uni.setStorageSync('loginIdPre','')
							uni.navigateTo({
								url:'../register/register?menuId='+menuId+'&modId='+modId+'&pageName='+pageName+'&templetName='+templetName
							})
						}else{
							uni.showModal({
							    title: '提示',
							    content: '系统设置不正确',
								showCancel:false
							})
						}
						
					},
					fail: function(res) {
						getApp().globalData.pic_address = 'https://www.hbxtwl.com/'
						getApp().globalData.app_address = 'https://www.hbxtwl.com/pbapi/'
						getApp().globalData.fileService = 'pbfile'
					}
				});
			
			},
		}
	};
</script>
<style>
.main-view{width: 100vw;height:100vh; background-color:cornflowerblue; background-size: 100% 100%;background-position:  center top;}
.center_view {width: 80%;  margin: 20rpx auto;margin-top: 200px; display: flex;flex-direction: column;}
.center_view input { padding: 10px;}
.item {display: flex;}
.item-img{display: flex;align-self: center;font-size: 18px;color:#ff9d29;}
.item-img2{display: flex;align-self: center;font-size: 18px;}
.item-picker{padding: 10px;display: flex;flex:1;}
.center_text{color:#f00;flex: 1;}
.item-picker-view{display: flex;flex: 1 1 auto;min-width: 80vw;color:#f00;}
.line{height: 2px;width: 100%;background: #eee;} 
.register{margin-top: 20px;background: #ff9d29;color: #fff;}
</style>
